package logic

import (
	"MyBlogv2/Servers/UserAction/internal/svc"
	"MyBlogv2/Servers/UserAction/userAction"
	"MyBlogv2/blog-common/HttpCode"
	define2 "MyBlogv2/blog-common/define"
	"MyBlogv2/blog-common/helper"
	"MyBlogv2/blog-common/models"
	"context"
	"github.com/jinzhu/copier"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetCommentListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetCommentListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetCommentListLogic {
	return &GetCommentListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetCommentListLogic) GetCommentList(in *UserAction.UserActionRequest) (*UserAction.GetCommentListResponse, error) {
	typeOfComment := int(in.Type)
	typeId := int(in.TypeId)
	pageNum := int(in.PageNum)
	pageSize := int(in.PageSize)
	var commentVoList []models.ArticleCommentVo
	var total int64
	models.Db.Model(new(models.Comment)).
		Table("t_comment c").
		Select("c.*,u.nickname as user_nickname,u.avatar user_avatar").
		Joins("left join sys_user u on c.comment_user_id = u.id").
		Where("type = ? and type_id = ? and is_check = ?", typeOfComment, typeId, define2.COMMENT_IS_CHECK).
		Where("parent_id = 0").
		Order("created_at desc").
		Count(&total).
		Offset((pageNum - 1) * pageSize).
		Limit(pageSize).
		Find(&commentVoList)

	for i, v := range commentVoList {
		var likeCount int64
		models.Db.Model(new(models.Like)).Where("type = ? and type_id = ?", define2.LIKE_TYPE_COMMENT, v.Id).Count(&likeCount)
		commentVoList[i].LikeCount = int(likeCount)
		var childCommentVoList []models.ArticleCommentVo
		models.Db.Model(new(models.Comment)).
			Table("t_comment c").
			Select("c.*,u.nickname as user_nickname,u.avatar user_avatar,ru.nickname as reply_user_nickname").
			Joins("left join sys_user u on c.comment_user_id = u.id").
			Joins("left join sys_user ru on c.reply_user_id = ru.id").
			Where("parent_id = ?", v.Id).
			Find(&childCommentVoList)

		for j, c := range childCommentVoList {
			var likeCountOfChildren int64
			models.Db.Model(new(models.Like)).Where("type = ? and type_id = ?", define2.LIKE_TYPE_COMMENT, c.Id).Count(&likeCountOfChildren)
			childCommentVoList[j].LikeCount = int(likeCountOfChildren)
		}
		commentVoList[i].ChildComment = childCommentVoList
		commentVoList[i].ChildCommentCount = len(childCommentVoList)
	}
	var commentVoListResponse []*UserAction.ArticleCommentVo
	commentVoListResponse = CopyWithChildren(commentVoList)
	//copier.Copy(commentVoListResponse, &commentVoList)
	return &UserAction.GetCommentListResponse{
		Code:  int64(HttpCode.SUCCESS),
		Data:  commentVoListResponse,
		Total: total,
	}, nil
}

func CopyWithChildren(commentVoList []models.ArticleCommentVo) []*UserAction.ArticleCommentVo {
	var commentVoListResponse []*UserAction.ArticleCommentVo
	copier.Copy(&commentVoListResponse, &commentVoList)
	for i, v := range commentVoList {
		commentVoListResponse[i].CreateTime = helper.MyTimeToTimestamp(v.CreateTime)
		commentVoListResponse[i].ChildComment = CopyWithChildren(v.ChildComment)
	}
	return commentVoListResponse
}
